#!/usr/bin/make -f

pkg_name = temurin-19-jre
priority = 1912
jvm_tools = java jfr jrunscript jwebserver keytool rmiregistry
amd64_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2+7/OpenJDK19U-jre_x64_linux_hotspot_19.0.2_7.tar.gz
amd64_checksum = 7386e10c74f00a4382be0540bc0494854804ad79427d8a50ac77a4c7208ff348
arm64_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2+7/OpenJDK19U-jre_aarch64_linux_hotspot_19.0.2_7.tar.gz
arm64_checksum = 3653f9e5ad21e4744e5a655e243fba2895651029bee23f3d2366d5debc41a736
armhf_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2+7/OpenJDK19U-jre_arm_linux_hotspot_19.0.2_7.tar.gz
armhf_checksum = d4588e8c01ca60da2ceed68b7d43d2fd9ec3350b93043f0dabd0eb6cb03cb23d
ppc64el_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2+7/OpenJDK19U-jre_ppc64le_linux_hotspot_19.0.2_7.tar.gz
ppc64el_checksum = a30203431c7c21602227d39368c5af6e7abd19000d6da5562de7f3f5c57cbad5
s390x_tarball_url = https://github.com/adoptium/temurin19-binaries/releases/download/jdk-19.0.2+7/OpenJDK19U-jre_s390x_linux_hotspot_19.0.2_7.tar.gz
s390x_checksum = 59463543feb858dc2eabeea344491897806da5d09c3cbf5b7c93e86269e32a02

d = debian/$(pkg_name)
jvm_home = usr/lib/jvm
jvm_dir = $(pkg_name)-$(DEB_HOST_ARCH)
pkg_alias = $(pkg_name)-$(DEB_HOST_ARCH)

%:
	dh $@

override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_strip:
override_dh_auto_install:
	# Download and verify checksum of JRE
	wget --progress=dot:mega -O jre.tar.gz $($(DEB_HOST_ARCH)_tarball_url)
	echo '$($(DEB_HOST_ARCH)_checksum) jre.tar.gz' > jre.tar.gz.sha256.txt
	sha256sum -c jre.tar.gz.sha256.txt

	# Substitute variables.
	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/jinfo.in > debian/jinfo

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/postinst.in > debian/postinst

	sed \
		-e 's/@pkg_alias@/$(pkg_alias)/g' \
		-e 's/@jvm_dir@/$(jvm_dir)/g' \
		-e 's/@priority@/$(priority)/g' \
		-e 's/@jvm_tools@/$(jvm_tools)/g' \
	    debian/prerm.in > debian/prerm

	# Unpack JRE.
	dh_installdirs \
		$(jvm_home) \
		$(jvm_home)/$(jvm_dir)
	tar --strip-components=1 -C "$(d)/$(jvm_home)/$(jvm_dir)" -xf jre.tar.gz

	# Add jinfo file (alternatives support).
	cp debian/jinfo "$(d)/$(jvm_home)/.$(pkg_alias).jinfo"

	# Replace bundled cacerts and redirect to adoptium-ca-certificates.
	rm -f "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"
	ln -s /etc/ssl/certs/adoptium/cacerts "$(d)/$(jvm_home)/$(jvm_dir)/lib/security/cacerts"

override_dh_gencontrol:
	dh_gencontrol --
